<?php
declare (strict_types=1);

namespace app\course\service\base\admin;

use app\common\library\Excel;
use app\common\service\BaseService;
use app\course\logic\base\admin\CourseStudent as CourseStudentLogic;
use app\course\logic\record\admin\LearnRecord;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

class CourseStudent extends BaseService
{

    /**
     * @Inject()
     *
     * @var CourseStudentLogic
     */
    protected $courseStudentLogic;

    /**
     * @Inject()
     *
     * @var LearnRecord
     */
    protected $learnRecordLogic;

    /**
     * 获取学员列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-30
     *
     * @param int $course_id [description]
     * @return   [type]                                    [description]
     */
    public function getStudentList(int $course_id, int $course_type)
    {
        // 搜索功能
        $map  = [];
        $data = $this->courseStudentLogic->getList($course_id, $map, Request::get('phone/s', ''), $course_type)->bindAttr('user', ['avatar_url', 'nick_name', 'phone'])->hidden(['user']);
        // 点播课和班级课显示学员学习情况
        // $visible = $course_type == 1 || $course_type == 4;
        // 定义字段
        $keyList = [
            'user'         => ListItem::make('custom')->title('用户'),
            'learn_rate'   => ListItem::make()->title('学习进度')->visible(),
            'total_time'   => ListItem::make()->title('累计学习时长')->visible(),
            'lately_time'  => ListItem::make()->title('最近学习时间')->visible(),
            'study_status' => ListItem::make('custom')->title('学习状态')->visible(),
            'from_text'    => ListItem::make()->title('加入方式'),
            'create_time'  => ListItem::make('custom')->title('加入时间'),
            'expire_time'  => ListItem::make()->title('过期时间')
        ];

        $searchKey = [
            'phone' => FormItem::make('input')->title(false)->placeholder('学员手机号')->maxlength(30),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();

    }

    /**
     * 添加学员
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-30
     */
    public function addStudent(int $course_id)
    {
        $phone = Request::post('phone/s', '');

        $this->courseStudentLogic->addStudent($course_id, $phone);

        return $this->parseToData([], 1, '添加学员成功');
    }

    /**
     * 移除学员
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-30
     *
     * @param int $course_id  [description]
     * @param int $student_id [description]
     * @return   [type]                                     [description]
     */
    public function removeStudent(int $course_id, int $student_id)
    {
        $course_type = Request::route('course_type');
        $this->courseStudentLogic->removeStudent($course_id, $student_id, $course_type);

        return $this->parseToData([], 1, '移除学员成功');
    }

    /**
     * 导入学员
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-10
     *
     * @param int $course_id [description]
     * @return   [type]                                    [description]
     */
    public function importStudent(int $course_id)
    {
        $attach_id = Request::post('attach_id/d');
        $res       = $this->courseStudentLogic->doImportStudent($course_id, $attach_id);

        return $this->parseToData($res, 1, '导入学员成功');
    }

    /**
     * 学习详情
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/1
     *
     * @param int $course_id
     * @param int $student_id
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function learnInfo(int $course_id, int $student_id)
    {
        $data = $this->learnRecordLogic->getLearnInfo($student_id, $course_id);
        // 定义字段
        $keyList = [
            'section_title'    => ListItem::make()->title('课时名称'),
            'total_learn_time' => ListItem::make()->title('累计学习时长/分钟'),
            'create_time'      => ListItem::make()->title('首次学习时间'),
            'update_time'      => ListItem::make()->title('最近学习时间'),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 导出学员
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/1
     *
     * @param int $course_id
     */
    public function exportStudent(int $courseId, int $courseType)
    {
        $datas = $this->courseStudentLogic->getExportData($courseId, $courseType);
        // 获取课程名称
        $title   = \app\course\model\base\admin\Course::where('id', $courseId)->value('title');
        $xlsCell = [
            ['phone', '手机号'],
            ['learn_rate', '学习进度'],
            ['total_time', '累计学习时长'],
            ['lately_time', '最近学习时间'],
            ['learn_status_text', '学习状态'],
            ['from_text', '加入方式'],
            ['create_time', '加入时间']
        ];

        return Excel::exportExcel($title . '- 学习情况', $xlsCell, $datas);
    }
}
